home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / ScreenSavers / BackSpaceViews / WordsView.BackModule / WordsView.m < prev    next >
Text File  |  1995-06-12  |  5KB  |  256 lines

  1. #import "WordsView.h"
  2. #import <math.h>
  3. #import "wraps.h"
  4.  
  5. // this code is copyright Darcy Brockbank, 1993
  6. //
  7. // You may freely reuse and distribute this code in any way shape or
  8. // form, provided that this notice stays intact.
  9. //
  10. // darcy@hasc.ca, samurai@cs.mcgill.ca
  11. //
  12. // If you do improve this thing, send me a copy!
  13. //
  14. // - darcy
  15.  
  16. @implementation WordsView
  17.  
  18. #define MINX -50.0
  19. #define MINY -10.0
  20. #define SIZE 64.0
  21.  
  22. - writeThresholdDefault:sender
  23. {
  24.     char tmp[128];
  25.     sprintf(tmp,"%u",threshold);
  26.     NXWriteDefault([NXApp appName],"WordsSpeed",tmp);
  27.     return self;
  28. }
  29.  
  30. - writeClearAfterDefault:sender
  31. {
  32.     char tmp[128];
  33.     sprintf(tmp,"%d",clearAfter);
  34.     NXWriteDefault([NXApp appName],"WordsClearAfter",tmp);
  35.     return self;
  36. }
  37.  
  38.  
  39. - writeBackingDefault:sender
  40. {
  41.     char tmp[128];
  42.     sprintf(tmp,"%d",gray);
  43.     NXWriteDefault([NXApp appName],"WordsBacking",tmp);
  44.     return self;
  45. }
  46.  
  47. - setThreshold:sender
  48. {
  49.     threshold = [sender intValue];
  50.     [self perform:@selector(writeThresholdDefault:)
  51.         with:self
  52.         afterDelay:1000
  53.         cancelPrevious:YES];
  54.     return self;
  55. }
  56.  
  57. - setClearAfter:sender;
  58. {
  59.     clearAfter = [sender intValue];
  60.     [self perform:@selector(writeClearAfterDefault:)
  61.         with:self
  62.         afterDelay:1000
  63.         cancelPrevious:YES];
  64.     return self;
  65. }
  66.  
  67. - loadWords;
  68. {
  69.     NXStream *s = NXMapFile("/usr/dict/words",NX_READONLY);
  70.     NXGetMemoryBuffer(s,&words,&len,&maxlen);
  71.     fontNames = [[FontManager new] availableFonts];
  72.     if (fontNames) {
  73.         for(numFonts = 0;fontNames[numFonts];numFonts++);
  74.     }
  75.     return self;
  76. }
  77.  
  78. - prepFont
  79. {
  80.     unsigned num = ((unsigned)random()) % numFonts;
  81.     float size = SIZE; //floor(32.0 + (64.0 * ((float)random())/(float)INT_MAX));
  82.     font = [Font newFont:fontNames[num] size:size matrix:NX_IDENTITYMATRIX];
  83.     [font set];
  84.     return self;
  85. }
  86.  
  87. - drawSelf:(const NXRect *)r :(int)c
  88. {
  89.     if (!r || !c || c==2) return self;
  90.  
  91.     NXSetColor(NX_COLORBLACK);
  92.     NXRectFill(r);
  93.     return self;
  94. }
  95.  
  96. - prepColor;
  97. {
  98.     blue = ((float)((unsigned)random()))/(float)INT_MAX;
  99.     red = ((float)((unsigned)random()))/(float)INT_MAX;
  100.     green = ((float)((unsigned)random()))/(float)INT_MAX;
  101.     currentColor = NXConvertRGBToColor(red,blue,green);
  102.     return self;
  103. }
  104.  
  105.  
  106. - prepPosition;
  107. {
  108.     xpos = (((unsigned)random()) % (unsigned)((maxCoord.x-MINX))) +MINX;
  109.     ypos = (((unsigned)random()) % (unsigned)((maxCoord.y-MINY))) +MINY;
  110.     return self;
  111. }
  112.  
  113. - prepWord;
  114. {
  115.     unsigned i = ((unsigned)random()) % (maxlen-1);
  116.     int j;
  117.     for(;(words[i]!='\n' && i);i--);
  118.     for(j=0,i++;words[i]!='\n' && i<maxlen;i++,j++) currentWord[j]=words[i];
  119.     currentWord[j]='\0';
  120.     return self;
  121. }
  122.  
  123. - prep;
  124. {
  125.     if (iteration++ > clearAfter) {
  126.         NXSetColor(NX_COLORBLACK);
  127.         NXRectFill(&bounds);
  128.         iteration=0;
  129.     }
  130.     [self prepFont];
  131.     [self prepColor];
  132.     [self prepPosition];
  133.     [self prepWord];
  134.     return self;
  135. }
  136.  
  137. - setGray:sender
  138. {
  139.     gray = [[sender selectedCell] tag];
  140.     [self perform:@selector(writeBackingDefault:)
  141.         with:self
  142.         afterDelay:1000
  143.         cancelPrevious:YES];
  144.     return self;
  145. }
  146.  
  147. - oneStep
  148. {
  149.     static unsigned lastTime = 0;
  150.     unsigned thisTime = currentTimeInMs();
  151.     
  152.     if (thisTime-lastTime<threshold) return self;
  153.     lastTime = thisTime;
  154.     [self prep];
  155. #if 0
  156.     NXSetColor(NX_COLORBLACK);
  157.     PSmoveto(xpos-2.0,ypos-2.0);
  158.     PSshow(currentWord);
  159.     NXSetColor(currentColor);
  160.     PSmoveto(xpos,ypos);
  161.     PSshow(currentWord);
  162. #else
  163.     if (gray){
  164.         PSdostuff(currentWord,xpos-1.0,ypos+1.0,xpos+1.0,ypos-1.0,xpos,ypos,red,green,blue);
  165.     } else {
  166.         PSdostuffnowhite(currentWord,xpos-1.0,ypos+1.0,xpos+1.0,ypos-1.0,xpos,ypos,red,green,blue);
  167.     }
  168. #endif
  169.     return self;
  170. }
  171.  
  172. - (BOOL)useBufferedWindow
  173. {
  174.     return YES;
  175. }
  176.  
  177. - initFrame:(const NXRect *)frameRect
  178. {
  179.     const char *t;
  180.     [super initFrame:frameRect];
  181.     [self allocateGState];        // For faster lock/unlockFocus
  182.     [self setClipping:NO];        // even faster...
  183. //    buffer = [[NXImage alloc] init];
  184.     [self newViewSize];
  185.     srandom(time(0));
  186.     if (t = NXReadDefault([NXApp appName],"WordsSpeed")){
  187.         threshold = atoi(t);
  188.     } else {
  189.         threshold = 200;
  190.     }
  191.     if (t= NXReadDefault([NXApp appName],"WordsClearAfter")){
  192.         clearAfter = atoi(t);
  193.     } else {
  194.         clearAfter = 500;
  195.     }
  196.     if (t= NXReadDefault([NXApp appName],"WordsBacking")){
  197.         gray = atoi(t);
  198.     } else {
  199.         gray = 1;
  200.     }
  201.     return self;
  202. }
  203.  
  204. - sizeTo:(NXCoord)width :(NXCoord)height
  205. {
  206.     [super sizeTo:width :height];
  207.     [self newViewSize];
  208.     return self;
  209. }
  210.  
  211. - newViewSize
  212. {
  213.     if (oldSize.width == bounds.size.width &&
  214.             oldSize.height == bounds.size.height)
  215.     {
  216.         return self;
  217.     }else{
  218.         oldSize.width = bounds.size.width;
  219.         oldSize.height = bounds.size.height;
  220.     }
  221.     maxCoord.x = bounds.size.width-150.0;
  222.     maxCoord.y = bounds.size.height;
  223.     if (maxCoord.x < 0) maxCoord.x = 0;
  224.     if (maxCoord.y < 0) maxCoord.y = 0;
  225. //    size.height = SIZE+2.0;
  226. //    size.width = oldSize.width;
  227. //    [buffer setSize:&size];
  228.     if ([self window]){
  229.         [self display];
  230.     }
  231.     return self;
  232. }
  233.  
  234. - (const char *)windowTitle
  235. {
  236.     return "WordsView";
  237. }
  238.  
  239.  
  240. - inspector:sender
  241. {
  242.     char buf[MAXPATHLEN];
  243.  
  244.     if (!sharedInspectorPanel){
  245.         sprintf(buf,"%s/WordsView.nib",[sender moduleDirectory:"Words"]);
  246.         [NXApp loadNibFile:buf owner:self withNames:NO];
  247.         [self loadWords];
  248.         [speedSlider setIntValue:threshold];
  249.         [clearSlider setIntValue:clearAfter];
  250.         [grayMatrix selectCellWithTag:gray];
  251.     }
  252.     return sharedInspectorPanel;
  253. }
  254.  
  255. @end
  256.